package leetcode;

/**
 * @author Cheng Jun
 * Description: 将 LeetCode 数组的测试案例，转化成符合 Java的语法 的数组
 * @version 1.0
 * @date 2021/12/25 8:23
 */
public class ArrayUtil {

    public static void main(String[] args) {
        getCharTwoArr("[[\"5\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"],[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"],[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"],[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"],[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"],[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"],[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"],[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"],[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]");
    }

    /**
     *  [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],
     *  ["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],
     *  [".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
     *
     * @author Cheng Jun
     * @date 2021/12/25 8:45
     * @param source
     * @return char[][]
     */
    public static char[][] getCharTwoArr(String source){
        // 掐头去尾 去掉开头的 [[，结尾的 ]]
        String arrString = source.substring(2, source.length() - 2);
        // 获得 "5","3",".",".","7",".",".",".","." 等
        String[] arr = arrString.split("],\\[");
        char[][] res = new char[arr.length][];
        for (int i = 0; i < arr.length; i++) {
            // 获得 5","3",".",".","7",".",".",".",".
            String[] charArr = arr[i].substring(1, arr[i].length() - 1).split("\",\"");
            char[] item = new char[charArr.length];
            for (int j = 0; j < charArr.length; j++) {
                // String的本质是char数组
                item[j] = charArr[j].charAt(0);
            }
            res[i] = item;
        }
        return res;
    }

    /**
     * 获取 char 二维数组
     * [["a", "b"], ["c", "d"]]
     * [['a', 'b'], ['c', 'd']]
     * @author Cheng Jun
     * @date 2021/12/25 8:29
     * @param source
     * @return char[][]
     */
    public static char[][] getCharTwoArr(String[][] source){
        int row = source.length;
        if (row == 0) return new char[0][0];
        int col = source[0].length;
        char[][] res = new char[row][col];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                // String的本质是char数组，因为source的元素是 String 而且是length=1
                res[i][j] = source[i][j].charAt(0);
            }
        }
        return res;
    }


}
